#include <QApplication>
#include <iostream>
#include <QTimer>
#include <QStyleFactory>
#include <QDir>
#include <QDirIterator>
#include "ctkPluginFrameworkFactory.h"
#include "ctkPluginFramework.h"
#include "ctkPluginException.h"
#include "ctkPluginContext.h"
#include "ctkPluginFrameworkLauncher.h"
#include "imainwindow.h"
#include "eventlistener.h"
#include "Hello/hello_service.h"

int main(int argc, char* argv[])
{
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);
    app.setApplicationName("ctktest");
    QString path = QCoreApplication::applicationDirPath();
    // 在插件的搜索路径列表中添加一条路径
    ctkPluginFrameworkLauncher::addSearchPath(path + "/plugins");
    // 设置并启动 CTK 插件框架
    try {
        ctkPluginFrameworkLauncher::start("org.commontk.eventadmin");
    }
    catch (ctkException e)
    {
        std::cout << e.message().toStdString() << std::endl;
    }
    // 启动插件工厂
    ctkPluginFrameworkFactory* ctkFrameWorkFactory = new ctkPluginFrameworkFactory;
    QSharedPointer<ctkPluginFramework> framework= ctkFrameWorkFactory->getFramework();
    try{
        framework->init();
        framework->start();
    }
    catch(const ctkPluginException& e)
    {
        std::cout << "framework init fail" << std::endl;
    }

    ctkPluginContext* pluginContext = framework->getPluginContext();

    // 事件监听
    EventListener listener;
    pluginContext->connectFrameworkListener(&listener, SLOT(onFrameworkEvent(ctkPluginFrameworkEvent)));
    pluginContext->connectPluginListener(&listener, SLOT(onPluginEvent(ctkPluginEvent)));
// 过滤 ctkEventAdmin 服务
// QString filter = QString("(%1=%2)").arg(ctkPluginConstants::OBJECTCLASS).arg("org.commontk.eventadmin");
    pluginContext->connectServiceListener(&listener, "onServiceEvent"); //, filter);

    QString dir = QCoreApplication::applicationDirPath();
    dir += "/plugins/Core.dll";
    QUrl url = QUrl::fromLocalFile(dir);
    QSharedPointer<ctkPlugin> plugin;
    try
    {
        plugin = pluginContext->installPlugin(url);
        plugin->start(ctkPlugin::START_TRANSIENT);
    }catch(ctkPluginException e){
        std::cout << e.message().toStdString() << e.getType() << std::endl;
    }

    dir = QCoreApplication::applicationDirPath();
    dir += "/plugins/About.dll";
    url = QUrl::fromLocalFile(dir);
    QSharedPointer<ctkPlugin> pluginAbout;
    try
    {
        pluginAbout = pluginContext->installPlugin(url);
        pluginAbout->start(ctkPlugin::START_TRANSIENT);
    }catch(ctkPluginException e){
        std::cout << e.message().toStdString() << e.getType() << std::endl;
    }

    dir = QCoreApplication::applicationDirPath();
    dir += "/plugins/Hello.dll";
    url = QUrl::fromLocalFile(dir);
    QSharedPointer<ctkPlugin> pluginHello;
    try
    {
        pluginHello = pluginContext->installPlugin(url);
        pluginHello->start(ctkPlugin::START_TRANSIENT);
    }
    catch (ctkPluginException e) {
        std::cout << e.message().toStdString() << e.getType() << std::endl;
    }
    // 获取服务引用
    ctkServiceReference reference = pluginContext->getServiceReference<iMainWindow>();
    if (reference) {
        // 获取指定 ctkServiceReference 引用的服务对象
        iMainWindow* service = qobject_cast<iMainWindow*>(pluginContext->getService(reference));
        if (service != Q_NULLPTR)
        {
            // 调用服务
            service->popWindow();
        }
    }

    QObject::connect(qApp, &QApplication::aboutToQuit, [framework](){
        // 停止插件
        framework->stop();
    });
    return app.exec();
}
